package com.tianji.jersey.resource.client;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.representation.Form;
import com.tianji.jersey.resource.AnswerVO;
import com.tianji.jersey.resource.AnswerVoList;

public class ClentTest {
	private WebResource resource;
	private static Log log = LogFactory.getLog(ClentTest.class);
	private static String baseUrl = "http://192.168.1.175/jersey-test/jersey/client/";
	public static void main(String[] args){
//		String url = "http://192.168.1.175/jersey-test/jersey/jersey/media2";
//		String url = "http://192.168.1.175/jersey-test/jersey/flights";
//		HttpResourceCaller<AnswerVoList> caller = new HttpResourceCaller<AnswerVoList>(AnswerVoList.class,url);
//		Client client = Client.create();
//		WebResource resource = client.resource(url);
//		resource.accept("application/json");
//		 String flights = resource.accept("application/xml").get(String.class);
//		 System.out.println(flights);
//		String str = (String) resource.get(String.class);
//		AnswerVoList list = caller.get();
//		String list = caller.get();
		
//		System.out.println(list.getAnswerVoList().get(0).getAnswerTitle());
//		testGet1();
//		testPost1();
//		testPostForm();
		testDelete();
	}
	public static void  testGet1(){
		String url = baseUrl+"get1";
		HttpResourceClient<AnswerVoList> client = new HttpResourceClientImpl<AnswerVoList>(AnswerVoList.class,url);
		AnswerVoList list = client.get();
		log.debug("=========================================");
		log.debug("list title ===>"+list.getAnswerVoList().get(0).getAnswerTitle());
		HttpResourceClientImpl<String> client1 = new HttpResourceClientImpl<String>(String.class,url);
		
		log.debug("str-===============>"+client1.setAcceptType("application/json").get());
	}
	public static void testPost1(){
		String url = baseUrl+"post1";
		HttpResourceClient<IntResult> client = new HttpResourceClientImpl<IntResult>(IntResult.class,url);
		IntResult result = client.post(getList(2));
		log.debug("=========post1==>"+result.getValue());
	}
	public static void testPostForm(){
		String url = baseUrl+"postForm";
		HttpResourceClient<IntResult> client = new HttpResourceClientImpl<IntResult>(IntResult.class,url);
		Form form = new Form();
		form.add("name", "kang");
		form.add("age", 21);
		IntResult result = client.post(form);
		log.debug("IntResult================>"+result.getValue());
	}
	private static AnswerVoList getList(int size){
		AnswerVoList list = new AnswerVoList();
		for(int i=0;i<size;i++){
			AnswerVO vo = new AnswerVO();
			vo.setAnswerId(i);
			vo.setAnswerTitle("测试"+i);
			list.getAnswerVoList().add(vo);
		}
		return list;
	}
	public static void testDelete(){
		String url = baseUrl+"delete";
		HttpResourceClient client = new HttpResourceClientImpl(url);
		IntResult result = new IntResult();
		result.setValue(22);
		client.deleteNoReturn(result);
		log.debug("========post1==>"+result.getValue());
	}
}
